import urlConfig from './config.js'
import tui from '../tui.js'
import constant from '../../constant/constant.js'
const fetch = {
	toast: function(tips) {
		uni.showToast({
			title: tips || "出错啦~",
			icon: 'none',
			duration: 2000
		})
	},
	request: function(url, postData, method, type, hideLoading) {
	
		//const token = this.cacheHelper.getToken()
		// console.log('地址',url);
		// console.log('参数',postData);
		//接口请求
		if (!hideLoading) {
			uni.showLoading({
				mask: true,
				title: '请稍候...'
			})
		}
		let SessionId = uni.getStorageSync(constant.StorageName.APP_LOGIN_TOKEN)
		console.log(SessionId);
		if(url=='/api/shop/passport/sendSms'){
			return new Promise((resolve, reject) => {
				uni.request({
					url: urlConfig + url,
					data: {
						...postData,
						appId:10001
					},
					header: {
						'content-type': type ? 'application/x-www-form-urlencoded' : 'application/json',
						'Tokenshop': SessionId
					},
					method: method, //'GET','POST'
					dataType: 'json',
					success: (res) => {
						resolve(res)
						// console.log(res);
						!hideLoading && uni.hideLoading()
						// if (res.data.code == 200 && res.data.success == true) {
						// 	resolve(res.data.data)
						// } else {
						// 	switch (res.data.code) {
						// 		case 401:
						// 			res.data.message = '登录超时请重新登录'
						// 			//清除 Token 用户信息并返回登录页
						// 			uni.setStorageSync(constant.StorageName.APP_LOGIN_TOKEN, "")
						// 			uni.setStorageSync(constant.StorageName.APP_USER_INFO, "")
						// 			//通知用户刷新
						// 			uni.$emit(constant.EventKey.UPDATE_USER_INFO, {
						// 				msg: '用户信息更新'
						// 			})
						// 			this.toast(res.data.message)
						// 			// uni.navigateTo({
						// 			// 	url: '/pages/client/login/login'
						// 			// });
						// 			break
						// 	}
						// 	reject(res.data)
						// }
					},
					fail: (res) => {
						if (!hideLoading) {
							reject(res)
							this.toast("网络不给力，请稍后再试~")
						}
					}
				})
			})
		}else{
			return new Promise((resolve, reject) => {
				uni.request({
					url: urlConfig + url,
					data: {
						...postData,
						appId:10001
					},
					header: {
						'Content-Type': type ? 'application/x-www-form-urlencoded' : 'application/json',
						'Tokenshop': SessionId
						// 'session_id': SessionId != null ? SessionId:'',
			
						// 'security': 1
					},
					method: method, //'GET','POST'
					dataType: 'json',
					success: (res) => {
						console.log(res,'resolve111')
						// 如果是 -1 表示未登录需要跳转到登录页面
						const { code } = res.data
						console.log(code,'code')
						if(code == -1){
									 uni.navigateTo({
										 	url: '/pages/login/login'
										 });
										 
						}else{
							resolve(res)
						}
						// console.log(res);
						!hideLoading && uni.hideLoading()
						// if (res.data.code == 200 && res.data.success == true) {
						// 	resolve(res.data.data)
						// } else {
						// 	switch (res.data.code) {
						// 		case 401:
						// 			res.data.message = '登录超时请重新登录'
						// 			//清除 Token 用户信息并返回登录页
						// 			uni.setStorageSync(constant.StorageName.APP_LOGIN_TOKEN, "")
						// 			uni.setStorageSync(constant.StorageName.APP_USER_INFO, "")
						// 			//通知用户刷新
						// 			uni.$emit(constant.EventKey.UPDATE_USER_INFO, {
						// 				msg: '用户信息更新'
						// 			})
						// 			this.toast(res.data.message)
						// 			// uni.navigateTo({
						// 			// 	url: '/pages/client/login/login'
						// 			// });
						// 			break
						// 	}
						// 	reject(res.data)
						// }
					},
					fail: (res) => {
						if (!hideLoading) {
							reject(res)
							this.toast("网络不给力，请稍后再试~")
						}
					}
				})
			})
		}
	},
    requestParams:function(url,params, method, hideLoading) {
		const token =uni.getStorageSync(constant.StorageName.APP_LOGIN_TOKEN)
		let nurl =urlConfig + url
		const htmls =[]
		Object.keys(params).forEach(key=>{
			htmls.push(`${key}=${params[key]}&`)
		})
		 htmls.push('appId=10001')
		if (htmls.length>0)
		    htmls.unshift('?')
		nurl+=htmls.join('')
		return new Promise((resolve, reject) => {
			uni.request({
				url: nurl,
				method: method, //'GET','POST'
				dataType: 'json',
				header:{
					'Tokenshop':token
				},
				success: (res) => {
					resolve(res)
					!hideLoading && uni.hideLoading()
				},
				fail: (res) => {
					if (!hideLoading) {
						reject(res)
						this.toast("网络不给力，请稍后再试~")
					}
				}
			})
		})
	},
	uploadFile: function(src) {
		const that = this
		uni.showLoading({
			title: '请稍候...'
		})
		return new Promise((resolve, reject) => {
			const uploadTask = uni.uploadFile({
				url: 'https://abc.cc',
				filePath: src,
				name: 'file',
				header: {
					'content-type': 'multipart/form-data'
				},
				formData: {},
				success: function(res) {
					uni.hideLoading()
					let d = JSON.parse(res.data)
					if (d.code === 1) {
						let fileObj = JSON.parse(d.data)[0];
						//文件上传成功后把图片路径数据提交到服务器，数据提交成功后，再进行下张图片的上传
						resolve(fileObj)
					} else {
						that.toast(res.message);
					}
				},
				fail: function(res) {
					reject(res)
					uni.hideLoading();
					that.toast(res.message);
				}
			})
		})
	},
	upload:function(url,formData) {
		const token =uni.getStorageSync(constant.StorageName.APP_LOGIN_TOKEN)
	  return new Promise((resolve,reject)=>{
		  uni.uploadFile({
			  url:urlConfig+url,
			  file:formData,
			  header:{
			  	'Tokenshop':token
			  },
			  dataType: 'json',
			  name:'iFile',
			  success:function(res){
				  const data =res.data
				  resolve(JSON.parse(data))
			  },
			  fail:function(er){
				  reject(er)
			  }
			 
			//  files:
		  })
		 //  uni.request({
		 //  	url:urlConfig+url,
			// method:'POST',
			// data:formData,
			// header:{
			// 	'Content-Type':'multipart/form-data',
			// 	'Tokenshop':token
			// }
		 //  }).then(res=>{
			//   resolve(res)
		 //  }).catch(error=>{
			//   reject(error)
		 //  })
	  })
		
	}

}

module.exports = {
	request: fetch.request,
	requestParams:fetch.requestParams,
	toast: fetch.toast,
	upload: fetch.upload
}
